#include "thread_pool.hpp"
extern MYSQL *conn;
extern Zhuce login_msg;
static void get_setting(char *setting, char *passwd)
{
    int i, j;
    for (i = 0, j = 0; passwd[i] && j != 4; i++)
    {
        if (passwd[i] == '$')
            ++j;
    }
    strncpy(setting, passwd, i);
}
void loginCheck1(user_info_t *user, task_t *task)
{
    // printf("loginCheck1.\n");
    // train_t t;
    // int ret;
    // memset(&t, 0, sizeof(t));
    // struct spwd *sp = getspnam(user->name);
    // if (sp == nullptr)
    // {
    //     t.len = 0;
    //     t.type = TASK_LOGIN_SECTION1_RESP_ERROR;
    //     ret = sendn(user->sockfd, &t, 8);
    //     printf("check1 send %d bytes.\n", ret);
    //     return;
    // }
    // char setting[100] = {0};
    // strcpy(user->encrypted, sp->sp_pwdp);
    // get_setting(setting, sp->sp_pwdp);
    // t.len = strlen(setting);
    // t.type = TASK_LOGIN_SECTION1_RESP_OK;
    // strncpy(t.buff, setting, t.len);
    // ret = sendn(user->sockfd, &t, 8 + t.len);
    // printf("check1 send %d bytes.\n", ret);
    epollDelReadFd(task->epfd, user->sockfd);
    train_t t;
    memset(&t, 0, sizeof(t));
    strcpy(user->name, task->data);
    int ret = find_name(conn, user->name, NULL);
    if (ret == 0)
    {
        t.len = 0;
        t.type = LOGIN_NO;
        sendn(user->sockfd, &t, 8);
        epollAddReadFd(task->epfd, user->sockfd);
    }
    else
    {
        get_salt(t.buff);
        t.len = strlen(t.buff) + 1;
        t.type = LOGIN_POK;
        sendn(user->sockfd, &t, 8 + t.len);
    }
    memset(&t, 0, sizeof(t));
    ret = recvn(user->sockfd, &t.len, 4);
    ret = recvn(user->sockfd, &t.type, 4);
    ret = recvn(user->sockfd, &t.buff, t.len);
    if (t.type == LOGIN_Q)
    {
        Zhuce login_msg;
        memset(&login_msg, 0, sizeof(login_msg));
        memcpy(&login_msg, t.buff, t.len);
        add_user(conn, login_msg.name, login_msg.token, login_msg.passward);
        epollAddReadFd(task->epfd, user->sockfd);
    }
}
// encrypted是上一个版本的命名
void registCheck2(user_info_t *user, const char *encrypted, task_t *task)
{
    // printf("loginCheck2.\n");
    // int ret;
    // train_t t;
    // memset(&t, 0, sizeof(t));
    // if (!strcmp(encrypted, user->encrypted))
    // {
    //     user->status = STATUS_LOGIN;
    //     t.type = TASK_LOGIN_SECTION2_RESP_OK;
    //     t.len = strlen("/server/$ ");
    //     strcpy(t.buff, "/server/$ ");
    //     ret = sendn(user->sockfd, &t, 8 + t.len);
    //     printf("Login success.\n");
    // }
    // else
    // {
    //     t.type = TASK_LOGIN_SECTION2_RESP_ERROR;
    //     printf("Login failed.\n");
    //     ret = sendn(user->sockfd, &t, 8);
    // }
    // printf("check2 send %d bytes.\n", ret);
    epollDelReadFd(task->epfd, user->sockfd);
    train_t t;
    memset(&t, 0, sizeof(t));
    strcpy(user->name, task->data);
    char tmp[300] = {0};
    int ret = find_name(conn, user->name, tmp);
    if (ret == -1)
    {
        t.len = 0;
        t.type = REGISTER_NO;
        epollAddReadFd(task->epfd, user->sockfd);
    }
    else
    {
        strcpy(t.buff, tmp);
        t.len = strlen(t.buff) + 1;
        t.type = REGISTER_POK;
    }
    ret = sendn(user->sockfd, &t, 8 + t.len);
    memset(&t, 0, sizeof(t));
    ret = recvn(user->sockfd, &t.len, 4);
    ret = recvn(user->sockfd, &t.type, 4);
    ret = recvn(user->sockfd, &t.buff, t.len);
    if (t.type == REGISTER_Q)
    {
        Zhuce login_msg;
        memset(&login_msg, 0, sizeof(login_msg));
        memcpy(&login_msg, t.buff, t.len);
        ret = math_user(conn, login_msg.name,
                        login_msg.passward, login_msg.token);
        if (ret == -1)
        {
            t.len = 0;
            t.type = REGISTER_NO;
        }
        else
        {
            t.len = 0;
            t.type = REGISTER_OK;
            char default_root[128] = {0};
            snprintf(default_root, sizeof(default_root), "./user/%s", login_msg.token, "user1");
            strncpy(user->pwd, default_root, sizeof(user->pwd) - 1);
        }
        sendn(user->sockfd, &t, 8 + t.len);
        epollAddReadFd(task->epfd, user->sockfd);
    }
    printf("登录成功\n");
}